package com.cdu.guoxinan.www.smartcom.service.impl;

import com.cdu.guoxinan.www.smartcom.domian.dto.QueryRoleDTO;
import com.cdu.guoxinan.www.smartcom.domian.pojo.Role;
import com.cdu.guoxinan.www.smartcom.mapper.RoleMapper;
import com.cdu.guoxinan.www.smartcom.mapper.UserMapper;
import com.cdu.guoxinan.www.smartcom.service.RoleService;
import com.cdu.guoxinan.www.smartcom.unit.model.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private UserMapper userMapper;
    @Override
    public R add(Role role) {
        roleMapper.add(role);
        return R.ok();
    }
    @Override
    public R query(QueryRoleDTO queryRoleDTO) {
        //设置分页信息
        PageHelper.startPage(queryRoleDTO.getPage(), queryRoleDTO.getLimit());
        List<Role> list = roleMapper.query(queryRoleDTO);
        PageInfo pageInfo = new PageInfo(list);
        return R.ok(pageInfo.getList(), pageInfo.getTotal());
    }

    @Override
    public R delete(int id) {
        //先将对应权限的用户变为普通用户
        userMapper.updateUserRole(id, 1);
        roleMapper.delete(id);
        return R.ok();
    }

    @Override
    public R update(Role role) {
        roleMapper.update(role);
        return R.ok();
    }

    @Override
    public R list() {
        List<Role> list = roleMapper.query( null);
        return R.ok(list);
    }
}
